﻿using BulkUploadGovernanceFiles.Models;
using BulkUploadGovernanceFiles.MongoSpecification;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace BulkUploadGovernanceFiles
{
    public class GovernanceProfileRepository
    {
        private readonly MongoCollection<GovernanceProfile> _governanceProfileCollection;

        public GovernanceProfileRepository()
        {
            var corporateDataConnectionString = ConfigManager.ClientDw.ConnectionString;

            var mongoClient = new MongoClient(corporateDataConnectionString);
            var server = mongoClient.GetServer();
            var database = server.GetDatabase(ConfigManager.ClientDw.DataBaseName);

            _governanceProfileCollection = database.GetCollection<GovernanceProfile>("GovernanceProfile");
        }
        
        public GovernanceProfile GetBy(ISpecification<GovernanceProfile> specification)
        {
            return _governanceProfileCollection.AsQueryable<GovernanceProfile>().FirstOrDefault(specification.SpecExpression);
        }

        public IEnumerable<GovernanceProfile> GetAll()
        {
            return _governanceProfileCollection.AsQueryable<GovernanceProfile>().ToList();
        }

        public long Count(ISpecification<GovernanceProfile> specification)
        {
            return _governanceProfileCollection.AsQueryable<GovernanceProfile>().OrderBy(specification.SpecExpression).Count(specification.SpecExpression);
        }
        public long Count()
        {
            return _governanceProfileCollection.AsQueryable<GovernanceProfile>().OrderBy(x=>x.CompanyName).Count();
        }
    }
}
